package com.briup.smartcity_group7.web.controller;

import com.briup.smartcity_group7.bean.basic.BaseUser;
import com.briup.smartcity_group7.service.IBaseUserService;
import com.briup.smartcity_group7.util.Result;
import com.briup.smartcity_group7.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: lenovo
 * @Date: 2023/01/05/12:30
 */
@Slf4j
@RequestMapping("/auth/user")
@Api(tags = "用户管理模块")
@RestController
public class UserController {
    @Autowired
    private IBaseUserService service;

    @ApiOperation("查询当前所登录的用户信息")
    @GetMapping("/info")
    public Result info(@RequestHeader("Authorization") String token){
        BaseUser user = service.currentUserInfo(token);
        return ResultUtil.success(user);
    }


    @ApiOperation("分页查询所有用户")
    @GetMapping("/page")
    public Result findByPage(@RequestParam(defaultValue ="1") Integer pageNum,
                             @RequestParam(defaultValue = "4") Integer pageSize){
        PageInfo<BaseUser> pageInfo = service.findAll(pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    public Result delete(Integer id){
        service.deleteUserById(id);
        return ResultUtil.success("删除用户成功");
    }

    @ApiOperation("更新用户信息")
    @PutMapping
    public Result update(BaseUser baseUser){
        service.updateUserById(baseUser);
        return ResultUtil.success("更新成功");
    }

    @ApiOperation("更新用户头像")
    @PutMapping("/icon")
    public Result updateIcon(Integer id,String url){
        service.updateIconById(id,url);
        return ResultUtil.success("更新成功");
    }
//    public Result updateIcon(BaseUser baseUser){
//        service.updateIconById(baseUser);
//        return ResultUtil.success("更新成功");
//    }
}
